﻿/*

The MIT License

Copyright (c) 2010 Cartesian Analytics, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

using System;
using System.Collections.Generic;
using System.Linq;

namespace Pipra.Uom.Core
{

    public class CoreTemperatureAuthority :
        IUomAuthority
    {
        private const double EntryValueCelsiusToKelvin = 273.15;
        private const double EntryValueKelvinToCelsius = -EntryValueCelsiusToKelvin;
        private static readonly ConversionFactorEntry EntryKelvinToRankine = new ConversionFactorEntry(9, 5);
        private static readonly ConversionFactorEntry EntryRankineToKelvin = ConversionFactorEntry.GetInverted(EntryKelvinToRankine);
        private const double EntryValueFahrenheitToRankine = 459.67;
        private const double EntryValueRankineToFahrenheit = -EntryValueFahrenheitToRankine;
        private const double EntryValueCelsiusToFahrenheitOffset = 32;
        private const double EntryValueFahrenheitToCelsiusOffset = -EntryValueCelsiusToFahrenheitOffset;

        private readonly UomComparer _cmp;
        private readonly Uom UomCelsius;
        private readonly Uom UomKelvin;
        private readonly Uom UomRankine;
        private readonly Uom UomFahrenheit;

        private readonly Uom[] ExposedUoms;

        private readonly Uom UomCelsiusFahrenheitIntermediary;

        public CoreTemperatureAuthority(UomComparer cmp)
        {
            if(null == cmp)
            {
                throw new ArgumentNullException("cmp");
            }
            _cmp = cmp;
            UomCelsius = new Uom(NameConstants.NameCelsius,this);
            UomKelvin = new Uom(NameConstants.NameKelvin,this);
            UomRankine = new Uom(NameConstants.NameRankine,this);
            UomFahrenheit = new Uom(NameConstants.NameFahrenheit,this);
            ExposedUoms = new[] { UomCelsius, UomFahrenheit, UomKelvin, UomRankine };
            UomCelsiusFahrenheitIntermediary = new Uom(NameConstants.NameCelsiusFahrenheitIntermediary,this);
        }

        public string Type
        {
            get { return NameConstants.NameTemperature; }
        }


        public IEnumerable<IUom> Units
        {
            get { return ExposedUoms.AsEnumerable().Cast<IUom>(); }
        }

        public IUom GetUnit(string name)
        {
            return Units.FirstOrDefault(uom => _cmp.Equals(uom.Name, name));
        }


        public UomComparer Comparer
        {
            get { return _cmp; }
        }


        public IEnumerable<IUom> GetMappedUnits(IUom from)
        {
            return
                _cmp.Equals(from, UomKelvin)
                    ? new[]{UomCelsius,UomRankine}
                : _cmp.Equals(from, UomCelsius)
                    ? new[]{UomKelvin,UomFahrenheit}
                : _cmp.Equals(from, UomRankine)
                    ? new[]{UomKelvin,UomFahrenheit}
                : _cmp.Equals(from, UomFahrenheit)
                    ? new[]{UomRankine,UomCelsius}
                :
                    Enumerable.Empty<IUom>()
            ;
        }

        public IValueConverter GetConverter(IUom from, IUom to)
        {
            if (_cmp.Equals(from, UomKelvin))
            {
                if (_cmp.Equals(to, UomCelsius))
                {
                    return new OffsetValueConverter(Type, from, to, EntryValueKelvinToCelsius);
                }
                if (_cmp.Equals(to, UomRankine))
                {
                    return FactorValueConverter.CreateConverter(Type, from, to, EntryKelvinToRankine);
                }
            }
            else if (_cmp.Equals(from, UomCelsius))
            {
                if (_cmp.Equals(to, UomKelvin))
                {
                    return new OffsetValueConverter(Type, from, to, EntryValueCelsiusToKelvin);
                }
                if (_cmp.Equals(to, UomFahrenheit))
                {
                    return new ValueConverterList(new IValueConverter<double>[]{
						FactorValueConverter.CreateConverter(Type,from,UomCelsiusFahrenheitIntermediary,EntryKelvinToRankine),
						new OffsetValueConverter(Type,UomCelsiusFahrenheitIntermediary,to,EntryValueCelsiusToFahrenheitOffset)
					});
                }
            }
            else if (_cmp.Equals(from, UomRankine))
            {
                if (_cmp.Equals(to, UomKelvin))
                {
                    return FactorValueConverter.CreateConverter(Type, from, to, EntryRankineToKelvin);
                }
                if (_cmp.Equals(to, UomFahrenheit))
                {
                    return new OffsetValueConverter(Type, from, to, EntryValueRankineToFahrenheit);
                }
            }
            else if (_cmp.Equals(from, UomFahrenheit))
            {
                if (_cmp.Equals(to, UomRankine))
                {
                    return new OffsetValueConverter(Type, from, to, EntryValueFahrenheitToRankine);
                }
                if (_cmp.Equals(to, UomCelsius))
                {
                    return new ValueConverterList(new IValueConverter<double>[]{
						new OffsetValueConverter(Type,from,UomCelsiusFahrenheitIntermediary,EntryValueFahrenheitToCelsiusOffset),
						FactorValueConverter.CreateConverter(Type,UomCelsiusFahrenheitIntermediary,to,EntryRankineToKelvin)
					});
                }
            }
            return null;
        }

    }
}
